/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package processor;

import entity.Department;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

/**
 *
 * @author maynoigi
 */
@Stateless
public class DepartmentFacade extends AbstractFacade<Department> {
    @PersistenceContext(unitName = "ManTechHelpdesk-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public DepartmentFacade() {
        super(Department.class);
    }
    
    public List<Department> getDepartmentAll() {
        return em.createQuery("SELECT d FROM Department d ORDER BY d.departmentID DESC").getResultList();
    }
    
    public Department getDepartment(Integer departmentID){
        try {
            Department de = (Department)em.createNamedQuery("Department.findByDepartmentID")
                    .setParameter("departmentID", departmentID).getSingleResult();
            return de;
        } catch (NoResultException e) {
            System.out.println(e.getMessage());
            return null;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
    
    public Department getDepartment(String departmentName){
        try {
            Department de = (Department)em.createNamedQuery("Department.findByDepartmentName")
                    .setParameter("departmentName", departmentName).getSingleResult();
            return de;
        } catch (NoResultException e) {
            System.out.println(e.getMessage());
            return null;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
    
    public boolean createDepartment(String departmentName) {
        try {
            Department de = new Department();
            de.setDepartmentName(departmentName);
            em.persist(de);
            return true;
        } catch(EntityExistsException e){
            System.out.println(e.getMessage());
            return false;
        } catch(Exception e){
            System.out.println(e.getMessage());
            return false;
        }
    }
    
    public boolean editDepartment(Integer departmentID,String departmentName) {
        try {
            Department de = getDepartment(departmentID);
            de.setDepartmentName(departmentName);
            em.merge(de);
            return true;
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
            return false;
        } catch(Exception e){
            System.out.println(e.getMessage());
            return false;
        }
    } 
    
    public boolean removeDepartment(Integer departmentID){
        try {
            Department de = getDepartment(departmentID);
            em.remove(de);
            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }
}
